﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WGM
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            themesetting();

            window_main.Title = "Protogamez WGM - Alpha release version " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }

        public void themesetting()
        {
            Elysium.Theme theme = Elysium.Theme.Light;

            SolidColorBrush ContrastColor = new SolidColorBrush();
            ContrastColor.Color = Color.FromArgb(255, 255, 255, 255);

            switch (Properties.Settings.Default.tscolor)
            {
                case "Light":
                    theme = Elysium.Theme.Light;
                    break;
                case "Dark":
                    theme = Elysium.Theme.Dark;
                    break;
            }

            switch (Properties.Settings.Default.tcolor)
            {
                case "Blue":
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Blue, ContrastColor);
                    break;
                case "Lime":
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Lime, ContrastColor);
                    break;
                case "Green":
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Green, ContrastColor);
                    break;
                case "Magenta":
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Magenta, ContrastColor);
                    break;
                case "Mango":
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Mango, ContrastColor);
                    break;
                case "Orange":
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Orange, ContrastColor);
                    break;
                case "Pink":
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Pink, ContrastColor);
                    break;
                case "Purple":
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Purple, ContrastColor);
                    break;
                case "Red":
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Red, ContrastColor);
                    break;
                case "Rose":
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Rose, ContrastColor);
                    break;
                case "Sky":
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Sky, ContrastColor);
                    break;
                case "Violet":
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Violet, ContrastColor);
                    break;
                case "Viridian":
                    Elysium.Manager.Apply(Application.Current, theme, Elysium.AccentBrushes.Viridian, ContrastColor);
                    break;
            }
        }

        private void window_main_Loaded(object sender, RoutedEventArgs e)
        {
            if (Properties.Settings.Default.screen == "1")
            {
                WindowState = WindowState.Maximized;
            }

            System.Windows.Forms.NotifyIcon ni = new System.Windows.Forms.NotifyIcon();
            ni.Icon = new System.Drawing.Icon("PWGM.ico");
            ni.Text = "Protogamez WGM";
            ni.Visible = true;
        }

        int buttonpress = 0;
        private void image_game_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            buttonpress = 1;

            Storyboard sb = this.FindResource("framemove") as Storyboard;
            sb.Begin();

            appbar.IsOpen = false;
        }

        private void image_stats_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            buttonpress = 2;

            loading1.Visibility = Visibility.Visible;
            loading2.Visibility = Visibility.Visible;

            Storyboard sb = this.FindResource("framemoveloading") as Storyboard;
            sb.Begin();

            appbar.IsOpen = false;
        }

        private void image_backup_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            buttonpress = 3;

            Storyboard sb = this.FindResource("framemove") as Storyboard;
            sb.Begin();

            appbar.IsOpen = false;
        }

        private void image_setting_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            buttonpress = 4;

            Storyboard sb = this.FindResource("framemove") as Storyboard;
            sb.Begin();

            appbar.IsOpen = false;
        }

        private void image_about_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            buttonpress = 5;

            Storyboard sb = this.FindResource("framemove") as Storyboard;
            sb.Begin();

            appbar.IsOpen = false;
        }

        private void image_license_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            buttonpress = 6;

            Storyboard sb = this.FindResource("framemove") as Storyboard;
            sb.Begin();

            appbar.IsOpen = false;
        }

        private void image_information_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            buttonpress = 7;

            Storyboard sb = this.FindResource("framemove") as Storyboard;
            sb.Begin();

            appbar.IsOpen = false;
        }

        private void image_update_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            appbar.IsOpen = false;

            string remoteUri = "https://dl.dropboxusercontent.com/s/vdw080h0j4erats/currentrelease.txt?token_hash=AAGUOIxBjGPHWkGKbsKWvFFiL0zVO5Gpk20nsd_ZsEGEEw&dl=1";
            string fileName = "currentrelease.txt", myStringWebResource = null;

            WebClient myWebClient = new WebClient();
            using (myWebClient)
            {
                myStringWebResource = remoteUri + fileName;
                myWebClient.DownloadFile(myStringWebResource, fileName);
            }

            String rversion;
            using (StreamReader sr = new StreamReader("currentrelease.txt"))
            {
                rversion = sr.ReadToEnd();

            }

            string cversion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

            if (window_main.Title == "Protogamez WGM - Beta release version " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() || window_main.Title == "Protogamez WGM - Alpha release version " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString())
            {
                frame_main.Visibility = Visibility.Hidden;
                textbox_status.Visibility = Visibility.Visible;
                pb_download.Visibility = Visibility.Visible;
                button_download.Visibility = Visibility.Visible;
                textbox_status.Text = "This feature is not supported in Beta phase";
                button_download.Content = "Close";
            }
            else
            {
                if (cversion != rversion)
                {
                    frame_main.Visibility = Visibility.Hidden;
                    textbox_status.Visibility = Visibility.Visible;
                    pb_download.Visibility = Visibility.Visible;
                    button_download.Visibility = Visibility.Visible;
                    button_later.Visibility = Visibility.Visible;
                    textbox_status.Text = "Update available";
                    button_download.Content = "Download";
                }
                else
                {
                    frame_main.Visibility = Visibility.Hidden;
                    textbox_status.Visibility = Visibility.Visible;
                    pb_download.Visibility = Visibility.Visible;
                    button_download.Visibility = Visibility.Visible;
                    textbox_status.Text = "No new update";
                    button_download.Content = "Close";
                }

                File.Delete("currentrelease.txt");
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            frame_main.Visibility = Visibility.Visible;
            textbox_status.Visibility = Visibility.Hidden;
            pb_download.Visibility = Visibility.Hidden;
            button_download.Visibility = Visibility.Hidden;
            button_later.Visibility = Visibility.Hidden;

            if (button_download.Content == "Download")
            {
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.EnableRaisingEvents = false;

                proc.StartInfo.FileName = "https://prototype85wgm.codeplex.com/releases/";
                proc.Start();

                this.Close();
            }
        }

        private void button_later_Click(object sender, RoutedEventArgs e)
        {
            frame_main.Visibility = Visibility.Visible;
            textbox_status.Visibility = Visibility.Hidden;
            pb_download.Visibility = Visibility.Hidden;
            button_download.Visibility = Visibility.Hidden;
            button_later.Visibility = Visibility.Hidden;
        }

        private void framemove_completed(object sender, EventArgs e)
        {
            switch (buttonpress)
            {
                case 1:
                    frame_main.Navigate(new Uri("PageGames.xaml", UriKind.Relative));
                    break;
                case 3:
                    frame_main.Navigate(new Uri("PageBackup.xaml", UriKind.Relative));
                    break;
                case 4:
                    frame_main.Navigate(new Uri("PageSetting.xaml", UriKind.Relative));
                    break;
                case 5:
                    frame_main.Navigate(new Uri("PageAbout.xaml", UriKind.Relative));
                    break;
                case 6:
                    frame_main.Navigate(new Uri("PageLicense.xaml", UriKind.Relative));
                    break;
                case 7:
                    frame_main.Navigate(new Uri("PageInformation.xaml", UriKind.Relative));
                    break;
            }

            Storyboard sb1 = this.FindResource("framemoveCom") as Storyboard;
            sb1.Begin();
        }

        private void framemove_completedloading(object sender, EventArgs e)
        {
            switch (buttonpress)
            {
                case 2:
                    frame_main.Navigate(new Uri("PageStats.xaml", UriKind.Relative));
                    break;
            }

            Storyboard sb1 = this.FindResource("framemoveComloading") as Storyboard;
            sb1.Begin();
        }

        private void framemove_completedloading_hidding(object sender, EventArgs e)
        {
            loading1.Visibility = Visibility.Hidden;
            loading2.Visibility = Visibility.Hidden;
        }
    }
}
